package legacy;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.util.ArrayList;

import alexAI.Utils;

public class Debugger {

	private static Debugger logFiles;
	private ArrayList<String> logData;
	private ArrayList<String> debugData;

	/**
	 * This method is used to get the Singleton instance of the Debugger class
	 * @return the Singleton instance of the Debugger class
	 */
	public static Debugger getInstance() {
		if(logFiles == null) 
			logFiles = new Debugger();
		return logFiles;
	}

	/**
	 * Private constructor that initializes the variables to be used by the Debugger class
	 */
	private Debugger() {
		logData = new ArrayList<String>();
		debugData = new ArrayList<String>();
	}

	/**
	 * <b>NON FONCTIONAL AT THE MOMENT</b> <br />
	 * The function appends the debugData to the file specified with fileName
	 * @param fileName the name of the file to write to *IMPORTANT the filename gets post-fixed
	 * with a time stamp
	 */
	public void appendDebugData(String fileName) {
		fileName += "_" + Utils.getTimeStamp();
		try {
			// Create file 
			FileWriter fstream = new FileWriter(fileName + ".debug",true);
			BufferedWriter out = new BufferedWriter(fstream);
			out.write("\r\n");
			for(int i = 0; i < debugData.size(); i++) {
				out.write(debugData.get(i) + "\r\n");
			}
			//Close the output stream
			out.close();
			//Clear data and reinitialize the array
			debugData.clear();
			debugData = new ArrayList<String>();
		}catch (Exception e){//Catch exception if any
			System.err.println("Error: " + e.getMessage());
		}
	}

	/**
	 * The returnDebugMsg returns a boolean to the function that called it and adds a debug statement
	 * to the debugData for debugging purposes. Also prints the data string to the console in case the program
	 * crashes and is unable to write to the debug file (Shouldn't happen but does ATM).
	 * @param b the boolean that needs to be returned
	 * @param statement the debug statement to be added to the array
	 * @return boolean - the boolean that needs to be returned
	 */
	public boolean returnDebugMsg(boolean b, String statement) {
		System.out.println(statement);
		debugData.add(statement);
		return b;
	}
}
